/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './style.scss';
import tpl from './tpl.html';

import Vue from 'vue';
import filter from '../../../../../grsc-base/util/filter';
import modal from '../../../../../grsc-base/component/Modal.vue';
import help from '../../../../../grsc-base/component/Help.vue';
import Popover from '../../../../../grsc-base/component/Popover.vue';
import Validator from '../../../../../grsc-base/component/Validator/main';
import VerifyCode from '../../../../component/VerifyCode.vue';

import service from './service';

export default  Vue.extend({
    template: tpl,
    props: {
        onBindingSuccess: {
            type: Function,
            default() {
            }
        },
        amountAvailable: 0,
        coinTypeText: '',
    },
    components: {
        modal,
        help,
        Popover,
        Validator,
        VerifyCode
    },
    created() {
        //this.precision = coinConfig.assets[this.coinTypeText].withdrawPrecision;
        this.lockRules.lockAmount.max = this.amountAvailable;
        this.lockRules.lockAmount.tip = this.$t('lockPlan.lockable')+ ': ' + filter.floorFix(this.amountAvailable, this.precision) + ' ' + this.coinTypeText;
        this.lockRules.lockAmount.maxTip = this.$t('lockPlan.lockable')+ ': ' + filter.floorFix(this.amountAvailable, this.precision) + ' ' + this.coinTypeText;
    },
    mounted() {

    },
    watch: {},
    computed: {},
    data() {
        let amountRule = {
            tip: this.$t('lockPlan.lockable') + ': --',
            required: true,
            requiredTip: this.$t('lockPlan.lockAmountTip'),
            min: 1,
            minTip: this.$t('lockPlan.errLockAmountTip')
        };
        return {
            //amountAvailable: 0,
            //coinTypeText: 'ACT',
            precision: 0,
            lockForm:{
                lockAmount: '',
                paypwd: '',
                identifyingCode: ''
            },
            lockRules:{
                lockAmount: amountRule,
                paypwd: {
                    required: true,
                    requiredTip: this.$t('common.assetPassRequireTip')
                },

                identifyingCode: {
                    required: true,
                    requiredTip: this.$t('common.verifyCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.IDENTIFY[this.$i18n.locale]
                }
            },
            isRequesting: false
        }
    },
    methods: {
        onSendSuccess() {
            this.lockRules.identifyingCode.$setRight(this.$t('common.codeSentTip'));
        },
        onSendFail(rep) {
            this.lockRules.identifyingCode.$setError(rep.msg);
        },


        submit(){
            if(this.lockRules.$checkAll()===false){
                return;
            }

            this.isRequesting = true;

            let param = {
                assetCode: this.coinTypeText,
                amount: this.lockForm.lockAmount
            };

            let header = {
                'pay-password': this.lockForm.paypwd,
                'service-code': this.lockForm.identifyingCode
            };
            /*
            if(this.googleStatus === 'ON'){
                header['google-code'] = this.idForm.googleCode
            }*/

            service.lock(param, header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.hide();
                    window.success(this.$t('common.success'));
                    this.onBindingSuccess();
                } else {
                    if (rep.code === window.CODE.PAY_PWD_FORMAT.CODE) {
                        this.lockRules.paypwd.$setError(window.CODE.PAY_PWD_FORMAT[this.$i18n.locale]);
                    } else if (rep.code === window.CODE.IDENTIFY_INVALID.CODE) {
                        setTimeout(() => {
                            this.lockRules.identifyingCode.$setError(window.CODE.IDENTIFY_INVALID[this.$i18n.locale]);
                        }, 0);
                    } else if (rep.code === window.CODE.PAY_PWD.CODE) {
                        let num = rep.data.num;
                        if (3 - num > 0) {
                            this.lockRules.paypwd.$setError(this.$t('common.IncorrectAssetPassTip',{num: 3-num}));
                        } else {
                            this.lockRules.paypwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                        }
                    } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
                        this.lockRules.paypwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                    } else if(rep.code === window.CODE.IDENTIFY.CODE){
                        // this.pwdForm.password = '';
                        // this.pwdForm.confirmPwd = '';
                        setTimeout(() => {
                            this.lockRules.identifyingCode.$setError(window.CODE.IDENTIFY[this.$i18n.locale]);
                        }, 0);
                    } else {
                        window.error(rep.msg);
                    }
                }
            }).always(()=> {
                this.isRequesting = false;
            });
        },
        hide() {
            this.$emit('hide');
        }
    }

});
